/**
 * Created by 121 on 2016/4/08.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog',
    'calendar',
    'highCharts'
], function ($, common, ready, template, Collection, Model, dialog, calendar, highCharts) {
    var index = function () {
        this.init();
    };
    $('.sidebar').css({'display': 'none'});
    $('header').css({'display': 'none'});
    $('.main').css({'margin': '50px 50px'});
    index.prototype = {
        init: function () {
            this.event();
            this.fetch();
        },
        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'activity_sale_data',
                condition: {limit: 10, offset: 0, type: 'day'},
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();

            this.collection_all = new Collection({
                container: 'activity-data-list',
                template: 'activity-data-item',
                page_dom: 'pagination-all',
                url: _global.url.api + 'activity_all_sale_data',
                condition: {limit: 10, offset: 0, type: 'day'},
                callback: function () {
                    loaded();
                }
            });
            this.collection_all.fetch();
        },
        event: function () {
            var _this = this;
            $('.long_date').calendar();
            console.log(ids, start_time, end_time);
            $.ajax({
                url: _global.url.api + 'activity_day',
                type: 'get',
                data: {ids: ids, start_time: start_time, end_time: end_time},
                success: function (res) {
                    $('#gd_curve').highcharts({
                        chart: {
                            zoomType: 'xy'
                        },
                        title: {
                            text: ''
                        },
                        xAxis: [{
                            //tickInterval: res.data.tickInterval,
                            //showFirstLabel: res.data.showFirstLabel,
                            categories: res.data.tab,
                            title: {
                                text: res.data.title_x,
                                style: {
                                    color: '#89A54E'
                                }
                            },
                            labels: {
                                rotation: -45,
                                align: 'right',
                                style: {
                                    fontSize: '10px'
                                }
                            }
                        }],
                        yAxis: [{ // Primary yAxis
                            allowDecimals: false,
                            floor: 0,
                            labels: {
                                format: '{value}',
                                style: {
                                    color: '#89A54E'
                                }
                            },
                            title: {
                                text: '',
                                style: {
                                    color: '#89A54E'
                                }
                            }
                        }],
                        tooltip: {
                            shared: true
                        },
                        legend: {
                            layout: 'vertical',
                            align: 'right',
                            verticalAlign: 'middle',
                            backgroundColor: '#FFFFFF'
                        },
                        series: res.data.series
                    });
                }
            });
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                var start_time = $('.start-time').val();
                var end_time = $('.end-time').val();
                //var id = new Array();
                //$("input[name='curve-list']:checked").each(function () {
                //    id.push($(this).val());
                //});
                var ids = $('.activity-id').val();
                console.log(start_time, end_time, ids);
                $.ajax({
                    url: _global.url.api + 'activity_day',
                    type: 'get',
                    data: {ids: ids, start_time: start_time, end_time: end_time},
                    success: function (res) {
                        $('#gd_curve').highcharts({
                            chart: {
                                zoomType: 'xy'
                            },
                            title: {
                                text: ''
                            },
                            xAxis: [{
                                tickInterval: res.data.tickInterval,
                                //showFirstLabel: res.data.showFirstLabel,
                                categories: res.data.tab,
                                title: {
                                    text: res.data.title_x,
                                    style: {
                                        color: '#89A54E'
                                    }
                                },
                                labels: {
                                    rotation: -45,
                                    align: 'right',
                                    style: {
                                        fontSize: '10px'
                                    }
                                }
                            }],
                            yAxis: [{ // Primary yAxis
                                allowDecimals: false,
                                floor: 0,
                                labels: {
                                    format: '{value}',
                                    style: {
                                        color: '#89A54E'
                                    }
                                },
                                title: {
                                    text: '',
                                    style: {
                                        color: '#89A54E'
                                    }
                                }
                            }],
                            tooltip: {
                                shared: true
                            },
                            legend: {
                                layout: 'vertical',
                                align: 'right',
                                verticalAlign: 'middle',
                                backgroundColor: '#FFFFFF'
                            },
                            series: res.data.series

                        });
                    }
                });
            });
            //查看销量明细
            $('#activity-sale-detail').on('click', function (e) {
                $('#panel-one').css({display: 'block'});
                e.preventDefault();
                _this.collection.condition.start_time = $('.start-time').val();
                _this.collection.condition.end_time = $('.end-time').val();
                _this.collection.condition.ids = $('.activity-id').val();
                _this.collection.condition.type = 'type';
                console.log(_this.collection);
                _this.collection.reload();

                var start_time = $('.start-time').val();
                var end_time = $('.end-time').val();
                var ids = $('.activity-id').val();
                $.ajax({
                    url: _global.url.api + 'activity_all_data_view',
                    type: 'get',
                    data: {ids: ids, start_time: start_time, end_time: end_time, type: 'day'},
                    success: function (res) {

                        console.log(res.data);
                        $('#data-view').html('');
                        $('#data-view').append(template('data-view-item', res.data))
                    }
                });
            });
            //查看销量统计
            $('#activity-sale-all').on('click', function (e) {
                $('#panel-two').css({display: 'block'});
                e.preventDefault();
                _this.collection_all.condition.start_time = $('.start-time').val();
                _this.collection_all.condition.end_time = $('.end-time').val();
                _this.collection_all.condition.ids = $('.activity-id').val();
                _this.collection_all.condition.type = 'day';
                console.log(_this.collection_all);
                _this.collection_all.reload();

                var start_time = $('.start-time').val();
                var end_time = $('.end-time').val();
                var ids = $('.activity-id').val();
                $.ajax({
                    url: _global.url.api + 'activity_all_data_view',
                    type: 'get',
                    data: {ids: ids, start_time: start_time, end_time: end_time, type: 'day'},
                    success: function (res) {
                        $('#data-view-all').html('');
                        console.log(res.data);
                        $('#data-view-all').append(template('data-view-item', res.data))
                    }
                });
            });
        }
    };
    new index();
});